<script setup name="home">
import { Search } from '@element-plus/icons-vue';
import { useStore } from 'vuex';
import { onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const store = useStore();
const isShow = ref(false);
store.dispatch('homepage/updateHeadList')
store.dispatch('homepage/updateCartList')
const allPrice = computed(() => {
  let price = 0;
  store.state.homepage.cartList.forEach((item) => {
    price += item.price * item.count;
  });
  return price;
});

const allCount = computed(() => {
  let count = 0;
  store.state.homepage.cartList.forEach((item) => {
    count += item.count;
  });
  return count;
});

// 获取首页列表
const homeList = computed(() => store.state.homepage.homeList)
// 滚动条事件
// 获取用户信息
const username = localStorage.getItem('account')
// 获取购物车数据
const cartList = computed(() => store.state.homepage.cartList)

const y = ref(0);
onMounted(() => {
  window.addEventListener('scroll', () => {
    y.value = parseInt(document.documentElement.scrollTop)
    if (y.value > 104) {
      isShow.value = true
    } else {
      isShow.value = false
    }
  });
});

// 跳转路由
function handleClick(id) {
  router.push({ name:'category', params: { id } })
  store.dispatch('homepage/updateItemList', id);
}
const goHome = () => {
  router.push('/')
}
</script>

<template>
  <header>
    <div class="header-wapper">
      <ul>
        <li>{{ username ? username : '请先登录' }}</li>
        <li>免费注册</li>
        <li>我的订单</li>
        <li>会员中心</li>
        <li>帮助中心</li>
        <li>关于我们</li>
        <li><el-icon><Iphone /></el-icon>手机版</li>
      </ul>
    </div>
  </header>
  <main>
    <div class="main-wapper">
      <div class="main-header">
        <a href=""></a>
        <ul>
          <li @click="goHome">首页</li>
          <el-dropdown
              v-for="(item, i) in homeList"
              :key="i"
              :max-height="132"
            >
            <li
              @click="handleClick(item.id)"
            >
              {{ item.name }}
            </li>
            <template #dropdown>
              <el-dropdown-menu
              >
              <el-dropdown-item
              v-for="(children, z) in item.children"
              :key="z"
              >
                <div class="menu-show">
                  <el-image style="width: 80px; height: 80px" :src="children.picture"/>
                  <div>{{ children.name }}</div>
                </div>
              </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </ul>
        <div class="serach">
          <el-input>
            <template #prefix>
              <el-icon color="black" :size="22"><Search /></el-icon>
            </template>
          </el-input>
        </div>
        <el-dropdown max-height="510px" >
          <el-badge :value="12" class="item">
            <el-icon :size="26"><ShoppingCartFull /></el-icon>
          </el-badge>
          <template #dropdown>
            <el-dropdown-menu v-if="cartList.length">
              <div class="cart">
                <div class="cart-goods">
                  <div
                  v-for="(item, i) in cartList"
                  class="cart-item"
                  >
                  <el-image style="width: 80px; height: 80px" :src="item.picture"/>
                  <div class="cart-desc">
                    <span>{{ item.name }}</span>
                    <div>{{ item.attrsText }}</div>
                  </div>
                  <div class="cart-money">
                    <span class="price">￥{{ item.nowPrice }}</span>
                    <span>X{{ item.count }}</span>
                  </div>
                  </div>
                </div>
                <div class="total">
                  <div>
                    <span> 共{{ allCount }}件商品</span>
                    <span class="price">￥{{ allPrice }}</span>
                  </div>
                  <button>去购物车结算</button>
                </div>
              </div>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
    <!-- 吸顶 -->
    <div class="affix" :class="{show: isShow}">
      <div class="affix-wapper">
        <div class="affix-header">
          <a href=""></a>
          <ul>
            <li>首页</li>
            <el-dropdown
                v-for="(item, i) in homeList"
                :key="i"
                :max-height="132"
              >
              <li>
                {{ item.name }}
              </li>
              <template #dropdown>
                <el-dropdown-menu
                >
                <el-dropdown-item
                v-for="(children, z) in item.children"
                :key="z"
                >
                  <div class="menu-show">
                    <el-image style="width: 80px; height: 80px" :src="children.picture"/>
                    <div>{{ children.name }}</div>
                  </div>
                </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </ul>
          <span>品牌</span>
          <span>专题</span>
        </div>
      </div>
    </div>
    <router-view />
  </main>
</template>

<style lang="scss" scoped>
header {
  height: 53px;
  width: 100%;
  background-color: black;
  display: flex;
  align-items: center;
  .header-wapper {
    width: 1240px;
    margin: 0 auto;
    ul {
      color: #fff;
      justify-content: right;
      font-size: 14px;
      li {
        border-right: 2px solid #fff;
        padding: 0 10px;
      }
    }
  }
}

ul {
  display: flex;
  li {
    list-style: none;
    display: flex;
    align-items: center;
    &:hover {
      color: #27ba9b;
      cursor: pointer;
    }
  }
}
.price {
  font-size: 18px;
  color: #cf4444;
}

main {
  width: 100vw;
  .main-wapper {
  width: 1240px;
  margin: 0 auto;

  .main-header {
    display: flex;
    align-items: center;
    height: 132px;
    background-color: #fff;
    a {
      display: inline-block;
      background: url(http://erabbit.itheima.net/img/logo.0940ebb5.png) no-repeat center 18px/contain;
      width: 200px;
      height: 132px;
    }
    ul {
      width: 820px;
      justify-content: space-around;
      li {
        padding-bottom: 5px;
        &:hover {
          border-bottom: 1px solid #27ba9b;
        }
      }
    }
  }
}
}
.cart {
  position: relative;
  height: 490px;
  .cart-goods {
    height: 440px;
    overflow: auto;
  }
  .cart-item {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    .cart-desc {
      width: 175px;
      padding: 0 10px;
    }
    .cart-money {
      padding: 0 10px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      text-align: center;
      font-size: 14px;
      color: #ccc;
    }
  }
  .total {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 70px;
    background-color: #f8f8f8;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      padding: 10px;
      flex-direction: column;
      justify-content: center;
      span {
        &:first-child {
          font-size: 14px;
          color: #ccc;
        }
      }
    }
    button {
      width: 180px;
      height: 50px;
      border: 1px solid #27ba9b;
      color: #27ba9b;
      border-radius: 5px;
      margin-right: 20px;
    }
  }

}

.affix {
  width: 100vw;
  background-color: #fff;
  transition: all .3s linear;
  position: fixed;
  top: 0;
  z-index: -1;
  opacity: 0;
  .affix-wapper {
    width: 1240px;
    margin: 0 auto;
    .affix-header {
      display: flex;
      align-items: center;
      height: 80px;
      background-color: #fff;
      a {
        display: inline-block;
        background: url(http://erabbit.itheima.net/img/logo.0940ebb5.png) no-repeat center 18px/contain;
        width: 200px;
        height: 132px;
      }
      ul {
        width: 820px;
        justify-content: space-around;
        li {
          padding-bottom: 5px;
          &:hover {
            border-bottom: 1px solid #27ba9b;
          }
        }
      }
      span {
        font-size: 14px;
        color: var(--el-text-color-regular);
        margin: 0 5px;
        margin-bottom: 5px;
        display: inline-block;
        border-left: 2px solid #27ba9b;
        padding: 0 10px;
        &:last-child {
          border: none;
        }
      }
    }
  }
}
.show {
  opacity: 1;
  z-index: 999;
}


:deep(.el-carousel__arrow) {
  width: 50px;
  height: 50px;
  .el-icon {
    font-size: 30px;
  }
}
:deep(.el-carousel__button) {
  width: 14px;
  height: 14px;
  border-radius: 50%;
}

</style>
